import headerWrap from "../commonComponents/header.vue"
import loadingWrap from "../commonComponents/loadingInPage.vue"
import actionSheet from "../commonComponents/actionSheet.vue"
import noMoreData from "../commonComponents/noMoreData.vue"

export default {
  data() {
    return {
      actionShow: false,
      switchFlag: 0,
      pendingPageIndex: 0,
      planList: [],
      detailPath: null,
      actionData: [
        {txt: "编辑", value: 1},
        {txt: "删除", value: 2},
      ],
    }
  },
  methods: {
    init() {
      this.lastShowFlag = this.$store.state.headerRightType;
      _methods.headerTitleChange(120);
      if (_showControl && _showControl['317002']){
        _methods.headerRightShow({
          val: 999,
          txt: ""
        });
      }
      setTimeout(() => {
        this.pendingListInit();
      }, 200);
    },
    pendingListInit() {
      let self = this;

      _data.planList = _methods.initMeScroll("planList", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          callback: () => {
            self.getList(1, 2);
          }
        }
      });

    },
    //列表
    getList(listFlag, scrollFlag) {
      /*
       listFlag：1.待办 2.已办 3.办结
       scrollFlag：1：刷新 2：加载更多
       */
      let self = this;
      //列表参数
      let listKey = {
        1: "planList",
      }[listFlag];
      //scrollKey
      let scrollKey = {
        1: "planList",
      }[listFlag];
      //页码参数
      let pageIndex = {
        1: "pendingPageIndex",
        2: "completePageIndex",
      }[listFlag];

      //待办 已办 办结，类型
      let state = {
        1: 1,
        2: 2,
      }[listFlag];

      let data = {
        userId: _data.userInfo.userId,
        pageSize: _data.pageSize,
        state,
      };
      //ajax获取列表数据
      let ajData = (s, pg = 1) => {
        data.pageIndex = pg;
        _ajax.noLoading(_ajaxUrls.planList.url, data, (v) => {
          let val = v.data.list;
          let len = val.length;
          if (s === 1) {
            //刷新
            self[listKey] = val;
          } else {
            //加载更多
            let arr = this[listKey];
            val.forEach((i) => {
              arr.push(i);
            });
            self[listKey] = arr;
          }
          _data[scrollKey].endSuccess(len, len == _data.pageSize);
        }, _ajaxUrls.planList.type);
      };

      if (scrollFlag === 1) {
        //刷新列表
        self[pageIndex] = 1;
        ajData(scrollFlag, self[pageIndex]);
      } else {
        // 加载更多
        self[pageIndex] = parseInt(self[pageIndex]) + 1;
        ajData(scrollFlag, self[pageIndex]);
      }
    },
    switchClick(i) {
      this.switchFlag = i;
    },
    editPlan() {
      // this.checkedItem
      let path = `/planList/planSubmit/planAdd?title=122&id=${this.checkedItem.reportId}`;
      _methods.goPath(path);
    },
    deletePlan() {
      let data = `/${this.checkedItem.reportId}?id=${this.checkedItem.reportId}`;
      _ajax.confirmWithLoading(_ajaxUrls.planDelete.url + data,
        {},
        () => {
          this.planList.splice(this.checkedIndex, 1);
        }, _ajaxUrls.planDelete.type, "是否确认删除？");
    },
    itemClick(item, index) {
      this.checkedItem = item;
      this.checkedIndex = index;
      this.actionShow = true;
    },
    actionClick(item) {
      switch (item) {
        case 1:
          this.editPlan();
          break;
        case 2:
          this.deletePlan();
          break;
      }
    }
  },
  components: {
    headerWrap,
    loadingWrap,
    actionSheet,
    noMoreData,
  },
  mounted() {
    this.init();
    _data.busInit.on_(() => {
      if (this.$store.state.headerRightType.val === 999){
        _methods.goPath("/planList/planSubmit/planAdd?title=121");
      }
    });
  },
  beforeDestroy() {
    _methods.headerRightShow(this.lastShowFlag);
  }
}
